import datetime
from java.awt import FlowLayout
from javax.swing.table import AbstractTableModel
from javax import swing

from model import player

from controller import playercontroller

pc = playercontroller()

def addplayerframe(event):
    apv = addplayerview()

class addplayerview(swing.JFrame):

    def __init__(self):
        swing.JFrame.__init__(self, "add player")
        self.contentPane.layout = FlowLayout()
        lblname = swing.JLabel('name')
        name = swing.JTextField(preferredSize=(200,20))
        save = swing.JButton('save', actionPerformed= lambda x, param=name: self.save(param))

        self.add(lblname)
        self.add(name)
        self.add(save)

        self.setSize(500, 70)
        self.pack()
        self.show()

    def save(self, obj):
        name = obj.text
        p = player(name, datetime.time())
        pc.addplayer(p)
        self.dispose()

class mainwindow(swing.JFrame):

    def __init__(self):
        swing.JFrame.__init__(self, "hattrick youth")
        self.size=(1024,768)
        
        # Create our layout
        self.contentPane.layout = FlowLayout()

        # Create the player list
        players = swing.JScrollPane(swing.JList(pc.listplayernames()))

        # Add the overview with the results
        pdm = playerdatamodel()
        playertable = swing.JScrollPane(swing.JTable(pdm))
        
        addplayer = swing.JButton('Add player', actionPerformed=addplayerframe)

        self.add(players)
        self.add(playertable)
        self.add(addplayer)
        self.pack()
        self.repaint()
        self.show()
    
        
class playerdatamodel(AbstractTableModel):

    COLS = ['match', 'date', 'position', 'playtime', 'rating']

    def getColumnCount(self):
        return len(self.COLS)

    def getRowCount(self):
        return 5

    def getColumnName(self, col):
        return self.COLS[col]

    def getValueAt(self, row, col):
        return 'blaat'

def main():
    mw = mainwindow()

if __name__ == '__main__':
    main()
